﻿//
// Copyright 2008-2011 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//

using System.Collections.Generic;

namespace YaTools.Yaml
{
    public sealed partial class Dump
    {
        public abstract partial class Presenter
        {
            /// <remarks>
            /// This class is an realization of the Strategy or State pattern.   Derrived classes
            /// should remain context free.   Add any required context to the Dump class.
            /// </remarks>
            public abstract partial class Yaml : Presenter
            {
                public override void WriteSeperation(Dump dump)
                {
                    Match(dump, Pending.DocumentHeader, "%YAML 1.2", Pending.EndDirectives, writeLine: true);
                    Match(dump, Pending.EndDirectives, "---", Pending.Space, writeLine: true);
                    base.WriteSeperation(dump);
                }

                protected static Dictionary<Tag, string> Shortcuts =
                    new Dictionary<Tag, string>
                        {
                            {Tag.Map, "!!map"},
                            {Tag.Sequence, "!!seq"},
                            {Tag.String, "!!str"}
                        };

                private static readonly char[] Linebreak = new[] {'\n'};

                public override void PresentAlias(Dump dump, string anchor)
                {
                    if (anchor != null)
                    {
                        RawWrite(dump, '*');
                        RawWrite(dump, anchor);
                    }
                }

                public override void PresentAnchor(Dump dump, string anchor)
                {
                    if (anchor != null)
                    {
                        dump.Write('&', anchor);
                    }
                }

                public override void PresentComment(Dump dump, string comment)
                {
                    RawWriteLine(dump);
                    foreach (string line in comment.Split(Linebreak))
                    {
                        dump.Write("# ");
                        dump.WriteLine(line);
                    }
                }
            }
        }
    }
}